﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RegExStudio.RegExStudioEventArgs;

namespace RegExStudio.UserControls
{
    /// <summary>
    /// Zeigt die Ergebnisse der RegEx an
    /// </summary>
    public partial class CtrlResult : CtrlGroup
    {
        private ResultsDataSet _dataSource;
        /// <summary>
        /// Datenquelle mit den Ergebnissen (ausser Replaced Text)
        /// </summary>
        public ResultsDataSet DataSource
        {
            get { return _dataSource; }
            set
            {
                if (value != null)
                {
                    _dataSource = value;
                    gridGroups.DataSource = _dataSource.Groups;
                    gridCaptures.DataSource = _dataSource.Captures;
                    gridSplits.DataSource = _dataSource.Splits;
                }
            }
        }
        
        /// <summary>
        /// Das Ergebniss des Replacens
        /// </summary>
        public string ReplacedText
        {
            get { return txtReplaced.Text; }
            set 
            {
                txtReplaced.Text = value; 
            }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public CtrlResult()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Laden des Controls
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CtrlResult_Load(object sender, EventArgs e)
        {
            //Kein Grid soll seine Spalten selbst erstellen
            gridGroups.AutoGenerateColumns = false;
            gridCaptures.AutoGenerateColumns = false;
            gridSplits.AutoGenerateColumns = false;
        }

        /// <summary>
        /// Setzt die Hintergrundfarbe der Zeile passend zur Farbe der Group
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridGroups_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            Color color = (Color)((System.Data.DataRowView)gridGroups.Rows[e.RowIndex].DataBoundItem).Row.ItemArray[6];
            gridGroups.Rows[e.RowIndex].DefaultCellStyle.BackColor = color; 
        }
        
        /// <summary>
        /// ContextMenu für die Groups öffnet sich
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuGroups_Opening(object sender, CancelEventArgs e)
        {
            if (_dataSource.Groups.Count > 0)
            {
                menuGroupsGoToInputToolStripMenuItem.Enabled = true;
                menuGroupsCopyValueToolStripMenuItem.Enabled = true;
                menuGroupsCopyAllToolStripMenuItem.Enabled = true;
            }
            else
            {
                menuGroupsGoToInputToolStripMenuItem.Enabled = false;
                menuGroupsCopyValueToolStripMenuItem.Enabled = false;
                menuGroupsCopyAllToolStripMenuItem.Enabled = false;
            }
        }

        /// <summary>
        /// Geht zur Stelle der Group in der Eingabe
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuGroupsGoToInputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = gridGroups.CurrentRow.Index;
            int index = (int)((System.Data.DataRowView)gridGroups.Rows[rowIndex].DataBoundItem).Row.ItemArray[3];
            int length = (int)((System.Data.DataRowView)gridGroups.Rows[rowIndex].DataBoundItem).Row.ItemArray[4]; 

            OnGoToSelection(this, new GoToSelectionEventArgs(index, length));
        }

        /// <summary>
        /// Kopiert den Wert der Group in die Zwischenablage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuGroupsCopyValueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = gridGroups.CurrentRow.Index;
            string value = ((System.Data.DataRowView)gridCaptures.Rows[rowIndex].DataBoundItem).Row.ItemArray[2].ToString();

            Clipboard.SetText(value, TextDataFormat.Text);
        }

        /// <summary>
        /// Kopiert die Tabelle der Groups in die Zwischenablage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuGroupsCopyAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = string.Empty;
            StringBuilder sb = new StringBuilder();

            string header = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", 
                                            colGroupsGrpNr.HeaderText, 
                                            colGroupsGrpName.HeaderText, 
                                            colGroupsValue.HeaderText,
                                            colGroupsIndex.HeaderText,
                                            colGroupsLength.HeaderText,
                                            colGroupsNumCap.HeaderText);

            sb.AppendLine(header);

            foreach (ResultsDataSet.GroupsRow dr in _dataSource.Groups)
            {
                string line = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", dr.GroupNr, dr.GroupName, dr.GroupValue, dr.GroupIndex, dr.GroupLength, dr.NumCaptures);
                sb.AppendLine(line);
            }       

            Clipboard.SetText(sb.ToString(), TextDataFormat.Text);
        }

        /// <summary>
        /// Setzt die Hintergrundfarbe der Zeile passend zur Farbe des Captures
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridCaptures_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            Color color = (Color)((System.Data.DataRowView)gridCaptures.Rows[e.RowIndex].DataBoundItem).Row.ItemArray[5];
            gridCaptures.Rows[e.RowIndex].DefaultCellStyle.BackColor = color;
        }

        /// <summary>
        /// ContextMenu für die Captures öffnet sich
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCaptures_Opening(object sender, CancelEventArgs e)
        {
            if (_dataSource.Captures.Count > 0)
            {
                menuCapturesGoToInputToolStripMenuItem.Enabled = true;
                menuCapturesCopyValueToolStripMenuItem.Enabled = true;
                menuCapturesCopyAllToolStripMenuItem.Enabled = true;
            }
            else
            {     
                menuCapturesGoToInputToolStripMenuItem.Enabled = false;
                menuCapturesCopyValueToolStripMenuItem.Enabled = false;
                menuCapturesCopyAllToolStripMenuItem.Enabled = false;
            }
        }

        /// <summary>
        /// Geht zur Stelle des Captures in der Eingabe
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCapturesGoToInputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = gridCaptures.CurrentRow.Index;
            int index = (int)((System.Data.DataRowView)gridCaptures.Rows[rowIndex].DataBoundItem).Row.ItemArray[3];
            int length = (int)((System.Data.DataRowView)gridCaptures.Rows[rowIndex].DataBoundItem).Row.ItemArray[4]; 

            OnGoToSelection(this, new GoToSelectionEventArgs(index, length));
        }

        /// <summary>
        /// Kopiert den Wert des Captures in die Zwischenablage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCapturesCopyValueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = gridCaptures.CurrentRow.Index;
            string value = ((System.Data.DataRowView)gridCaptures.Rows[rowIndex].DataBoundItem).Row.ItemArray[2].ToString();

            Clipboard.SetText(value, TextDataFormat.Text);
        }

        /// <summary>
        /// Kopiert die Tabelle der Captures in die Zwischenablage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCapturesCopyAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = string.Empty;
            StringBuilder sb = new StringBuilder();

            string header = string.Format("{0}\t{1}\t{2}\t{3}\t{4}", 
                                            colCapturesGrpNr.HeaderText, 
                                            colCapturesCapNr.HeaderText, 
                                            colCapturesValue.HeaderText, 
                                            colCapturesIndex.HeaderText, 
                                            colCapturesLength.HeaderText);

            sb.AppendLine(header);

            foreach (ResultsDataSet.CapturesRow dr in _dataSource.Captures)
            {
                string line = string.Format("{0}\t{1}\t{2}\t{3}\t{4}", dr.GroupNr, dr.CaptureNr, dr.CaptureValue, dr.CaptureIndex, dr.CaptureLength);
                sb.AppendLine(line);
            }

            Clipboard.SetText(sb.ToString(), TextDataFormat.Text);
        }
        
        /// <summary>
        /// Eventhandler wenn der gewünschte Bereich ausgewählt werden soll
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void GoToSelectionEventHandler(object sender, GoToSelectionEventArgs e);

        /// <summary>
        /// Event das gefeuert wird wenn der gewünschte Bereich ausgewählt werden soll
        /// </summary>
        public event GoToSelectionEventHandler GoToSelection;

        /// <summary>
        /// Feuert das Event wenn der gewünschte Bereich ausgewählt werden soll
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnGoToSelection(object sender, GoToSelectionEventArgs e)
        {
            if (GoToSelection != null)
            {
                GoToSelection(sender, e);
            }
        }

        /// <summary>
        /// ContextMenu für die Splits öffnet sich
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuSplits_Opening(object sender, CancelEventArgs e)
        {
            if (_dataSource.Splits.Count > 0)
            {
                menuSplitsCopyValueToolStripMenuItem.Enabled = true;
                menuSplitsCopyAllToolStripMenuItem.Enabled = true;
            }
            else
            {  
                menuSplitsCopyValueToolStripMenuItem.Enabled = false;
                menuSplitsCopyAllToolStripMenuItem.Enabled = false;
            }
        }

        /// <summary>
        /// Kopiert den gerade gewählten Wert der Splits in die Zwischenablage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuSplitsCopyValueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = gridSplits.CurrentRow.Index;
            string value = ((System.Data.DataRowView)gridSplits.Rows[rowIndex].DataBoundItem).Row.ItemArray[1].ToString();

            Clipboard.SetText(value, TextDataFormat.Text);
        }

        /// <summary>
        /// Kopiert alle Splits in die Zwischenablage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuSplitsCopyAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = string.Empty;
            StringBuilder sb = new StringBuilder();

            string header = string.Format("{0}\t{1}", colSplitsNr.HeaderText, colSplitsValue.HeaderText);

            sb.AppendLine(header);

            foreach (ResultsDataSet.SplitsRow dr in _dataSource.Splits)
            {
                string line = string.Format("{0}\t{1}", dr.SplitNr, dr.SplitValue);
                sb.AppendLine(line);
            }

            Clipboard.SetText(sb.ToString(), TextDataFormat.Text);
        }
    }
}
